package me.zhengjie.tool;

import org.springframework.core.NamedThreadLocal;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
 * 使用之后一定要remove*/
public class ErrorUtil {
    private static NamedThreadLocal<List<String>> error_msg = new NamedThreadLocal("当前线程业务错误信息");
    public static void addErrorMsg(String msg){
        if (error_msg.get() == null){
            error_msg.set(new ArrayList<>());
        }
        if (msg == null || "".equals(msg)){
            return;
        }
        error_msg.get().add(msg);
    }
    public static List<String> getErrorMsg(){
        List<String> errors = Optional.ofNullable(error_msg.get()).orElse(new ArrayList<>());
        error_msg.remove();
        return errors;
    }
    public static void clearErrorMsg(){
        error_msg.remove();
    }
}
